package com.example.minefieldkeepercloud.activity.ipreport;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.base.BaseApplication;

import com.example.minefieldkeepercloud.bean.IPLoginBean;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.TcpNetHelper;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.SPUtilHelper;
import com.example.minefieldkeepercloud.util.TitleControl;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.util.netty.INettyClient;
import com.example.minefieldkeepercloud.util.netty.NettyClient;
import com.example.minefieldkeepercloud.view.dialog.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * IP登录
 */
public class IPLoginActivity extends AppCompatActivity {
    @BindView(R.id.edit_phone)
    EditText edit_phone;//账户名

    @BindView(R.id.edit_password)
    EditText edit_password;//密码

    private INettyClient.OnDataReceiveListener onDataReceiveListener;
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iplogin);
        ButterKnife.bind(this);

        initTitle();


        edit_phone.setText(SPUtilHelper.getPhone());
        edit_password.setText(SPUtilHelper.getPwd());

        setSetDataReceiveListener();
        NettyClient.getInstance().connect(BaseApplication.IPADRESS, BaseApplication.PORT);
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle(getString(R.string.IPreportname));

        loadingDialog = new LoadingDialog(this, 0);
       Log.i("-->","Token==="+SPUtilHelper.getToken());
    }


    /**
     * 设置数据监听
     */
    private void setSetDataReceiveListener() {
        onDataReceiveListener = new INettyClient.OnDataReceiveListener() {
            @Override
            public void onDataReceive(int mt, String jsons) {

                Log.i("-->", "LoginActivity返回数据：" + mt +" json:"+ jsons);
                loadingDialog.dismiss();
//                handler.sendEmptyMessage(mt);

                if (mt==TcpNetHelper.CONNECTSUCC){
                    //连接成功
                    if(check())
                    {
                        loadingDialog.show();
                        TcpNetHelper.login(edit_phone.getText().toString().trim(),edit_password.getText().toString().trim());
                    }
                }
                //登录成功返回
                if (mt==TcpNetHelper.ReLogin)
                {

                    IPLoginBean loginBean = MyGson.getInstance().fromJson(jsons, IPLoginBean.class);
                    if (loginBean.isIsSuccess())
                    {
                        startActivity(new Intent(IPLoginActivity.this,IPReportActivity.class));

                        finish();
                    }else
                    {
                        ToastUtil.showToast(IPLoginActivity.this,loginBean.getMessage());
                    }

                }



            }
        };
        if (onDataReceiveListener!=null)
            NettyClient.getInstance().addDataReceiveListener(onDataReceiveListener);
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除绑定
        NettyClient.getInstance().delectDataReceiveListener(onDataReceiveListener);
    }

    @OnClick({R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login://登录
                if(check())
                {
                    loadingDialog.show();
                    TcpNetHelper.login(edit_phone.getText().toString().trim(),edit_password.getText().toString().trim());
                }
                break;


        }
    }

    private boolean check() {
        if ("".equals(edit_phone.getText().toString().trim()))
        {
            ToastUtil.showToast(this,"账号不能为空");
            return false;
        }
        if ("".equals(edit_password.getText().toString().trim()))
        {
            ToastUtil.showToast(this,"密码不能为空");
            return false;
        }
        if (!NetWorkUtil.checkNet(getApplicationContext())) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
            return false;
        }
        return true;
    }
}
